package com.dreamer.view.user;

import com.dreamer.domain.user.AccountsRecord;
import com.dreamer.domain.user.User;
import com.dreamer.domain.user.enums.AccountsType;
import com.dreamer.service.mobile.AccountsRecordHandler;
import com.dreamer.service.pyramid.AccountsRHandler;
import com.dreamer.util.ExcelFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import ps.mx.otter.utils.SearchParameter;
import ps.mx.otter.utils.WebUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.*;

/**
 * Created by huangfei on 06/07/2017.
 */
@Controller
@RequestMapping("/accounts/record")
public class AccountsRecordController {

    @Autowired
    private AccountsRHandler accountsRHandler;

    @RequestMapping("/index.html")
    public String index(@ModelAttribute("parameter") SearchParameter<AccountsRecord> parameter, Integer typeState, HttpServletRequest request, Model model){
       try {
           User user = (User) WebUtil.getCurrentUser(request);
           AccountsType accountsType = AccountsType.stateOf(typeState);
           parameter.getEntity().setAccountsType(accountsType);
           parameter.getEntity().setCanShow(true);
           List<AccountsRecord> accountsRecords = accountsRecordHandler.findAccountsRecords(parameter,user);
           //List<AccountsRecord> accountsRecords = accountsRHandler.searchAllByPage(parameter);
           WebUtil.turnPage(parameter, request);
           model.addAttribute("records",accountsRecords);
           model.addAttribute("accountsTypes",AccountsType.values());
           return "/user/accounts_record";
       }catch (Exception e){
           e.printStackTrace();
           model.addAttribute("error",e.getMessage());
           return "/mobile/error";
       }
    }

    @ResponseBody
    @RequestMapping(value = "/downRecord.html", method = RequestMethod.GET)
    public void download(
            @ModelAttribute("parameter") SearchParameter<AccountsRecord> parameter,Integer typeState,HttpServletRequest request, HttpServletResponse response) {
        try {
            List<AccountsRecord> pts;
            pts = accountsRHandler.searchAllByPage(parameter);
            Double  jin = 0.0;
            Double  chu = 0.0;
            List<Map>   datas1 = new ArrayList<>();
            List<Map>   datas2 = new ArrayList<>();
            List<String> headers1 = new ArrayList<>();
            headers1.add("姓名");
            headers1.add("电话");
            headers1.add("支出/进账");
            headers1.add("详情");
            headers1.add("操作数量");
            headers1.add("变更后数量");
            headers1.add("时间");
            List<String> headers2 = new ArrayList<>();
            headers2.add("进账");
            headers2.add("支出");
            Map m =null;
            for(AccountsRecord voucherRecord : pts) {
                m=new HashMap();
                m.put(0,voucherRecord.getAgent().getRealName());
                m.put(1,voucherRecord.getAgent().getMobile());
                m.put(2,voucherRecord.getAccountsType().getStateInfo());
                m.put(3,voucherRecord.getInfo());
                m.put(4,voucherRecord.getAmount());
                m.put(5,voucherRecord.getNowAmount());
                m.put(6,voucherRecord.getUpdateTime());
                datas1.add(m);
                if (voucherRecord.getAddSub().equals(0)) {
                    BigDecimal b1 = new BigDecimal(chu.toString());
                    BigDecimal b2 = new BigDecimal(voucherRecord.getNowAmount().toString());
                    chu=new Double(b1.subtract(b2).doubleValue());
                } else {
                    BigDecimal b1 = new BigDecimal(jin.toString());
                    BigDecimal b2 = new BigDecimal(voucherRecord.getNowAmount().toString());
                    jin=new Double(b1.add(b2).doubleValue());
                }
            }
            Map m1 = new HashMap();
            m1.put(0,jin);
            m1.put(1,chu);
            datas2.add(m1);
            List<String> ss=new ArrayList<>();
            ss.add("账户详情");
            List<List> hs=new ArrayList<>();
            hs.add(headers1);
            hs.add(headers2);
            List<List<Map>> ds=new ArrayList<>();
            ds.add(datas1);
            ds.add(datas2);
            ExcelFile.ExpExs("",ss,hs,ds,response);//创建表格并写入
        } catch (Exception exp) {
            exp.printStackTrace();
        }
    }


    @ModelAttribute("parameter")
    public SearchParameter<AccountsRecord> preprocess(@RequestParam("id") Optional<Integer> id) {
        SearchParameter<AccountsRecord> searchParameter = new SearchParameter<>();
        AccountsRecord parameter;
        if (id.isPresent()) {
            parameter = accountsRecordHandler.get(id.get());
        } else {
            parameter = new AccountsRecord();
        }
        searchParameter.setEntity(parameter);
        return searchParameter;
    }

    @Autowired
    private AccountsRecordHandler accountsRecordHandler;

}
